package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void save(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.save(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
//如果选择口味,就有,不选择,就没有
        if (!flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.insertBeach(flavors);
        }

    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult PageChect(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishFlavorMapper.PageChect(dishPageQueryDTO);
        long total = page.getTotal();


        return new PageResult(total, page.getResult());
    }


    /**
     * \
     * 批量删除
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        //在dish判断是否售卖
        log.info("删除id{}", ids);
        for (Integer id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }


        //在setmeal_dish判断是否存在
        List<Integer> setmealIds = setmealMapper.getSetmealDish(ids);
        if (!setmealIds.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }


        //删除dish和dish_flavor

        dishMapper.delete(ids);
        dishFlavorMapper.delete(ids);
    }

    @Override
    public DishVO selectById(Integer list) {

        Dish dish = dishMapper.selectById(list);

        List<DishFlavor> dishFlavorlist = dishFlavorMapper.selectById(list);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavorlist);

        return dishVO;
    }


    @Override
    public void Update(DishDTO dishDTO) {

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishDTO, dishVO);
        //修改基本表
        dishMapper.Update(dishVO);
        //新口味无需插入,口味表都是先删除后整体插入

        for (DishFlavor flavor : dishVO.getFlavors()) {
            flavor.setDishId(dishVO.getId());
        }
        //设置为空 (删除 不插入)
        //修改 update(删除 插入)
            dishFlavorMapper.flavordelete(dishVO);

        if (!dishVO.getFlavors().isEmpty()) {//数据为空抛出操作
            dishFlavorMapper.insertBeach(dishVO.getFlavors());
        }

    }

    @Override
    public void StartorStop(Integer status, Long id) {
        dishMapper.StartorStop(status,id);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectById(Math.toIntExact(d.getId()));

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }



}
